package donot996.icu;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpGetTest {
    public static void main(String[] args) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet对象，设置url访问地址
        HttpGet httpGet=new HttpGet("http://www.itcast.cn");
        CloseableHttpResponse response=null;
        try{
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //将实体转成string对象
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content.length());
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //关闭response
                response.close();
                httpClient.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}
